#include <tsin/SimpleArgument.hpp>

#include <cstring>
#include <string>

using namespace tsin;
using namespace GetOpt;

SimpleArgument::SimpleArgument(int argc, char *argv[])
        : ops(argc, argv)
{
        this->argc = argc;
        this->argv = argv;
}

int SimpleArgument::size()
{
        return argc;
}

char *SimpleArgument::getArgument(int index)
{
        if (index < 0 || index >= argc)
                return 0;
        return argv[index];
}

bool SimpleArgument::getOption(char shortName, const char *longName, char *buf)
{
        bool present;
        std::string _buf;

        if (!longName)
                longName = "";

        ops >> OptionPresent(shortName, longName, present);
        if (!present)
                return false;
        ops >> Option(shortName, longName, _buf);

        strcpy(buf, _buf.c_str());
        return true;
}

bool SimpleArgument::getOption(char shortName, const char *longName, int &opt)
{
        bool present;
        int _buf;

        if (!longName)
                longName = "";

        ops >> OptionPresent(shortName, longName, present);
        if (!present)
                return false;
        ops >> Option(shortName, longName, _buf);

        opt = _buf;
        return true;
}
